package com.zhxymm.book.controllers;

import com.zhxymm.book.dao.OverTimeRepository;
import com.zhxymm.book.entity.OverTime;
import com.zhxymm.book.service.OverTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("overTimes")
public class OverTimeController {

    @Autowired
    OverTimeRepository overTimeRepository;

    @Autowired
    OverTimeService overTimeService;

    @GetMapping
    public String displayOverTimes(Model model){
        List<OverTime> overTimes = overTimeRepository.findAll();
        model.addAttribute("overTimes",overTimes);
        return "overTimes/list-overTimes";
    }

    @GetMapping("/new")
    public String displayOverForm(Model model){
        OverTime aOverTime = new OverTime();
        model.addAttribute("overTime",aOverTime);
        return "overTimes/new-overTime";
    }

    @PostMapping("/save")
    public String createOverTime(Model model,OverTime overTime){
        overTimeRepository.save(overTime);
        return "redirect:/overTimes";
    }

    @GetMapping("/update")
    public String displayOverTimeUpdateForm(@RequestParam("id") long theId,Model model){


        OverTime theOverTime = overTimeService.findByOverTimeId(theId);
        model.addAttribute("overTime",theOverTime);
        return "overTimes/new-overTime";
    }

    @GetMapping("/delete")
    public String deleteOverTime(@RequestParam("id") long theId,Model model){

        OverTime theOverTime =overTimeService.findByOverTimeId(theId);
        overTimeService.delete(theOverTime);
        return "redirect:/overTimes";
    }
}
